package com.sihai.ehr.job.impl;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.ExamQuanItemSet;
import com.sihai.ehr.model.biz.MessageType;
import com.sihai.ehr.model.biz.TrainPlan;
import com.sihai.ehr.model.biz.TrainPlanDutyTarget;
import com.sihai.ehr.model.biz.TrainPlanEmployeeTarget;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.repository.AuditingRepository;

/**
 * 发送培训信息
 * 
 * @author henry
 * 
 */
public class SendTrainMessageJob {
	@Resource
	private DataAccessor dataAccessor;

	@Resource
	private AuditingRepository auditingRepository;

	// 定时器执行的方法
	public void execute() {
		System.out.println("henry----------execute send message job");
		sendTrainMessage();
	}

	// 发送定量考核待审核消息
	private void sendTrainMessage() {
		// 得到定量考核项
		List<TrainPlan> trainPlans = dataAccessor
				.findByHql(
						TrainPlan.class,
						"from TrainPlan t where t.sendMsgStatus=0 order by t.planStartDate ",
						null, null, null);
		if (trainPlans != null && !trainPlans.isEmpty()) {
			for (TrainPlan item : trainPlans) {
				if (item.getLecturerType() == 0) { // 内训
					// 读取培训员工
					List<EmployeeinfoWork> employeeinfoWorks = getEmploymentsByPlanId(
							item.getId(), null, null);
					if (employeeinfoWorks != null && employeeinfoWorks.size() > 0) {
						// MessageType type, String targetUrl, Long targetId,
						// Duty
						// duty, final EmployeeinfoWork work, String[] data
						// 发送定量数据输入消息
						String[] inputData = {
								MessageType.train_plan.getName(),
								item.getTitle() };
						Iterator<EmployeeinfoWork> it = employeeinfoWorks.iterator();
						while(it.hasNext()){
							EmployeeinfoWork work = it.next();
		
							auditingRepository.saveMessage(MessageType.train_plan,
									"train/plan_browse_main.xhtml", item.getId(),
									item.getDuty(), work.getBaseId(), inputData);
						}
					}
				} else { // 外训
					List<TrainPlanEmployeeTarget> employeeTargets = getTrainPlanEmployeeTargets(item.getId(), null, null);
					if (employeeTargets != null && employeeTargets.size() > 0) {
						// MessageType type, String targetUrl, Long targetId,
						// Duty
						// duty, final EmployeeinfoWork work, String[] data
						// 发送定量数据输入消息
						String[] inputData = {
								MessageType.train_plan.getName(),
								item.getTitle() };
						Iterator<TrainPlanEmployeeTarget> it = employeeTargets.iterator();
						while(it.hasNext()){
							TrainPlanEmployeeTarget target = it.next();
		
							auditingRepository.saveMessage(MessageType.train_plan,
									"train/outing_train_main.xhtml", item.getId(),
									item.getDuty(), target.getEmployeeinfoWork().getBaseId(), inputData);
						}
					}
				}
			}
		}
	}

	/**
	 * 读取内训员工
	 * 
	 * @param planId
	 * @param limit
	 * @param sorter
	 * @return
	 */
	private LimitedList<EmployeeinfoWork> getEmploymentsByPlanId(
			final Long planId, Limit limit, Sorter sorter) {
		try {
			return dataAccessor.findByQuery(EmployeeinfoWork.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from EmployeeinfoWork e where e.duty.id in (select t.duty.id from TrainPlanDutyTarget t where 1 <> 2");
							dynamicQuery.append("and t.trainPlan.id=" + planId
									+ ")");

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象员工时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getEmploymentsByPlanId;参数planId="
							+ planId + "】");
		}
	}

	/**
	 * 读取外训
	 * 
	 * @param planId
	 * @param limit
	 * @param sorter
	 * @return
	 */
	private LimitedList<TrainPlanEmployeeTarget> getTrainPlanEmployeeTargets(
			final Long planId, Limit limit, Sorter sorter) {
		try {
			return dataAccessor.findByQuery(TrainPlanEmployeeTarget.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainPlanEmployeeTarget t where 1 <> 2");
							dynamicQuery.append("and t.trainPlan.id=" + planId);

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训员工对象时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanEmployeeTargetServiceImpl.getTrainPlanEmployeeTargets;参数planId="
							+ planId + "】");
		}
	}
}
